<?php
/**
 * Created by PhpStorm.
 * User: chen
 * Date: 2016/1/14
 * Time: 15:08
 */
class SetScannerAction extends Action{
    public function _initialize(){
        import('ORG.Util.Session');
    }
    /**
     * 公共弹框方法
     */
    public function simpleAlert($content, $url = 'javascript:history.go(-1);')
    {
        echo "<SCRIPT language=JavaScript>alert('$content');location.href='" . $url . "';</SCRIPT>";
        echo ' ';
        exit;
    }
    public function index(){
        $adminId=trim(I('get.adminId'));
        if($adminId){
            session::set('adminId',$adminId);
        }else{
            $this->simpleAlert('id参数缺失');
        }
        $key=trim(I('get.key'));
        if($key){
            if($key!=md5($adminId.'guangjixinxi')){
                $this->simpleAlert('无效的二维码');
            }
        }else{
            $this->simpleAlert('key参数缺失');
        }
        $url=getBaseUrl().'/musihui/setScanner.php';
        redirect("https://open.weixin.qq.com/connect/oauth2/authorize?appid=wx08981071f73cb701&redirect_uri=$url&response_type=code&scope=snsapi_userinfo&state=123#wechat_redirect");
    }
    /**
     * 接收openid，并写入admin表
     */
    public function getInfo(){
        $openid=trim(I('get.openid'));
        $nickname=trim(I('get.nickname'));
        $key=trim(I('get.key'));
        if($key!=md5($openid.'guangjixinxi')){
            $this->simpleAlert('非法请求');
        }
        $adminId=session::get('adminId');
        $map=array();
        $map['adminid']=$adminId;
        $map['openid']=$openid;
        $rs = D('AdminScan')->where($map)->find();
        if($rs){
            echo '<SCRIPT language=JavaScript>alert("您已有权限，请不要重复扫码");</SCRIPT>';
            exit;
        }
        $data=array();
        $data['openid']=$openid;
        $data['nickname']=$nickname;
        $data['adminid']=$adminId;
        $data['createtime'] = time();
        D('AdminScan')->data($data)->add();
        echo '<SCRIPT language=JavaScript>alert("设置成功");</SCRIPT>';
    }
}